package com.first.module.acm.action;

import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import com.first.core.common.QueryFilter;
import com.first.core.web.action.BaseAction;
import com.first.module.acm.model.SystemConfig;
import com.first.module.acm.model.SystemLog;
import com.first.module.acm.service.SystemConfigService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class SystemConfigAction extends BaseAction {

	@Resource
	private SystemConfigService systemConfigService;
	private SystemConfig systemConfig;
	private String configId;

	public String getConfigId() {
		return configId;
	}

	public void setConfigId(String configId) {
		this.configId = configId;
	}

	public SystemConfig getSystemConfig() {
		return systemConfig;
	}

	public void setSystemConfig(SystemConfig systemConfig) {
		this.systemConfig = systemConfig;
	}

	public String list() {
		QueryFilter filter = new QueryFilter(getRequest());
		List<SystemConfig> list = this.systemConfigService.getAll(filter);

		Type type = new TypeToken<List<SystemConfig>>() {
		}.getType();
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
				.append(filter.getPagingBean().getTotalItems()).append(
						",result:");

		Gson gson = new Gson();
		buff.append(gson.toJson(list, type));
		buff.append("}");

		this.jsonString = buff.toString();

		return "success";
	}

	public String multiDel() {
		String[] ids = getRequest().getParameterValues("ids");
		if (ids != null) {
			for (String id : ids) {
				this.systemConfigService.remove(id);
			}
		}

		this.jsonString = "{success:true}";

		return "success";
	}

	public String get() {
		SystemConfig SystemConfig = (SystemConfig) this.systemConfigService.get(this.configId);

		Gson gson = new Gson();

		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(gson.toJson(SystemConfig));
		sb.append("}");
		setJsonString(sb.toString());

		return "success";
	}

	public String save() {
		this.systemConfigService.save(this.systemConfig);
		setJsonString("{success:true}");
		return "success";
	}
}
